#include<bits/stdc++.h>
#define ll long long
#define mxn 200003
#define md 1000000007
#define pb push_back
#define mkp make_pair
#define pq priority_queue
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define rept(i,a,b) for(int i=a;i<b;++i)
#define drep(i,a,b) for(int i=a;i>=b;--i)
#define umap unordered_map
using namespace std;
inline int read(){
    int x=0;bool fl=0;char c=getchar();
    while(!isdigit(c)){if(c=='-')fl=1;c=getchar();}
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return fl?-x:x;
}
int n,m,k,T,X[4],a[mxn],a1[mxn],c[mxn],d1[mxn<<1],d2[mxn<<1],d[20][mxn];
ll ans,f[mxn<<1],f1[mxn<<1][20],ff[20],as[mxn];
char st[mxn];
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    n=read(),m=read();
    rep(i,1,n)a1[i]=read();
    rep(i,1,m)c[i]=read();
    k=1;while(1<<k<n)k++;
    rept(i,0,k){
        scanf("%s",st);
        rept(s,0,1<<(k-i-1)){
            d[i][s]=st[s]-'0';
        }
    }  
    T=read();
    while(T--){
        rept(i,0,4)X[i]=read();
        rep(i,1,n)a[i]=a1[i]^X[i&3];
        rep(i,1,1<<k){
            f[(1<<k)-1+i]=i;
            rept(j,0,k)f1[(1<<k)-1+i][j]=i;
            d1[(1<<k)-1+i]=k-1,d2[(1<<k)-1+i]=0;
        }
        drep(i,(1<<k)-1,1){
            f[i]=f[i<<1]+f[(i<<1)|1];
            rept(j,0,k)f1[i][j]=f1[i<<1][j]+f1[(i<<1)|1][j];
            d1[i]=k-1,d2[i]=0;
        }
        ans=0;
        int k1=0;
        rep(i,1,n){
            while(1<<k1<i)k1++;
            int p=(1<<k)-1+i;
            rept(j,a[i],k)f1[p][j]=0;
            d1[p]=min(a[i]-1,k-1),d2[p]=min(a[i],k);
            rept(j,0,k){
                p>>=1;
                rept(x,0,k)ff[x]=f1[p][x];
                ll ff1=f[p];int dd1=d1[p],dd2=d2[p];
                if(!d[j][p-(1<<(k-j-1))]){
                    f[p]=(d1[p<<1]>=j?f1[p<<1][j]:0ll)+(d2[p<<1]<=j?f[p<<1|1]:0ll);
                    rept(x,0,k)f1[p][x]=(d1[p<<1]>=j?f1[p<<1][max(j,x)]:0ll)+(d2[p<<1]<=j?f1[p<<1|1][x]:0ll);
                    d1[p]=-1,d2[p]=k;
                    if(d1[p<<1]>=j)d1[p]=max(d1[p],d1[p<<1]),d2[p]=min(d2[p],d2[p<<1]);
                    if(d2[p<<1]<=j)d1[p]=max(d1[p],d1[p<<1|1]),d2[p]=min(d2[p],d2[p<<1|1]);
                }else{
                    f[p]=(d1[p<<1|1]>=j?f1[p<<1|1][j]:0ll)+(d2[p<<1|1]<=j?f[p<<1]:0ll);
                    rept(x,0,k)f1[p][x]=(d1[p<<1|1]>=j?f1[p<<1|1][max(j,x)]:0ll)+(d2[p<<1|1]<=j?f1[p<<1][x]:0ll);
                    d1[p]=-1,d2[p]=k;
                    if(d1[p<<1|1]>=j)d1[p]=max(d1[p],d1[p<<1|1]),d2[p]=min(d2[p],d2[p<<1|1]);
                    if(d2[p<<1|1]<=j)d1[p]=max(d1[p],d1[p<<1]),d2[p]=min(d2[p],d2[p<<1]);
                }
                rept(x,0,k)if(ff[x]!=f1[p][x])goto next;
                if(f[p]==ff1&&d1[p]==dd1&&d2[p]==dd2)break;
                next:;
            }
            as[i]=f[1<<(k-k1)];
        }
        rep(i,1,m)ans^=i*as[c[i]];
        cout<<ans<<'\n';
    }
    return 0;
}